﻿using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.WebNemo.Business.Extensionable {
    public class DaExtensionManager : IExtensionManager {

        #region Fields

        private ImpExtensionManager instance;
        private IGroup extensionsForGroup;
        private IDataAccess dataAccess;

        #endregion

        #region Properties

        public string GroupExtensionTypeDeclaration {
            get {
                return this.instance.GroupExtensionTypeDeclaration;
            }
        }

        public string GroupRecordExtensionTypeDeclaration {
            get {
                return this.instance.GroupRecordExtensionTypeDeclaration;
            }
        }

        #endregion

        #region Constructors

        public DaExtensionManager(IGroup group, IDataAccess dataAccess) {
            this.extensionsForGroup = group;
            this.dataAccess = dataAccess;
            this.instance = this.GetInstance();
        }

        #endregion

        #region Methods

        public void SetGroupExtension(Type groupExtensionType) {
            this.instance.SetGroupExtension(groupExtensionType);
            this.Save();
        }

        public void SetGroupRecordExtension(Type groupRecordExtensionType) {
            this.instance.SetGroupRecordExtension(groupRecordExtensionType);
            this.Save();
        }

        public void RemoveGroupExtension() {
            this.instance.RemoveGroupExtension();
            this.Save();
        }

        public void RemoveGroupRecordExtension() {
            this.instance.RemoveGroupRecordExtension();
            this.Save();
        }

        private ImpExtensionManager GetInstance() {
            ExtensionData data= this.dataAccess.GetExtensions(this.extensionsForGroup.Id);
            ImpExtensionManager instance = new ImpExtensionManager();
            if (data != null) {
                instance.GroupExtensionTypeDeclaration = data.GroupExtension;
                instance.GroupRecordExtensionTypeDeclaration = data.GroupRecordExtension;
            }
            return instance;
        }

        private void Save() {
            this.dataAccess.SaveExtensions(this.extensionsForGroup.Id, this.GroupExtensionTypeDeclaration, this.GroupRecordExtensionTypeDeclaration);
        }

        #endregion

    }
}
